package lwk.juc.lock;


/**
 * @author:lwk
 * @create: 2022-08-10 22:06
 * @Description: 经典卖票
 */
public class SafeTicketDemo1 {
    public static void main(String[] args) {
        Ticket ticket = new Ticket();

        new Thread(()->{
            for (int i = 0; i < 100; i++) {
                try {
                    ticket.sell();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        },"A窗口").start();
        new Thread(()->{
            for (int i = 0; i < 100; i++) {
                try {
                    ticket.sell();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        },"B窗口").start();
        new Thread(()->{
            for (int i = 0; i < 100; i++) {
                try {
                    ticket.sell();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        },"C窗口").start();


    }
}

class Ticket {
    private int ticketNums = 200;
    public synchronized void sell() throws InterruptedException {
        if (ticketNums>0) {
            System.out.println(Thread.currentThread().getName()+"卖出了第=="+ticketNums+"==张票，剩余===="+--ticketNums);
        }
    }
}